<?php
include("capcha/securimage.php");

if($_SERVER['REQUEST_METHOD'] != "POST")
{
@header('Location: index.php');
}

if(isset($_SESSION['user']))
{
	die("Вече сте регистриран!");
}

$born_month_array = array('Януари','Февруари','Март','Април','Май','Юни','Юли','Август','Септември','Октомври','Ноември','Декемеври');

if(isset($_POST['reg']))
{
	$errors = array();
	$born_day = $_POST['born_day'];
	$born_month = $_POST['born_month'];
	$born_year = $_POST['born_year'];
	$user = new UserForRegistration($_POST['user_name'], $_POST['pass'], $_POST['email'], $born_day, $born_month, $born_year, $_POST['gender']);
	$pass2 = addslashes($_POST['pass2']);
	$pass2 = addslashes($pass2);
	$img = new Securimage();
	$capcha = $img->check($_POST['capcha_v']);
	
	$errors["user_name"] = $user->validate_user_name();
	$errors["password"] = $user->validate_password();
	if($user->getPassword() != $pass2)
	{
		$errors["passmatch"] = "<font color=\"red\">Паролите не съвпадат!</font>";
	}
	else
	{
		$errors["passmatch"] = true;
	}
	$errors["email"] = $user->validate_email();
	if($born_day == '' || $born_month == '' || $born_year == '')
	{
		$errors["born_date"] = "<font color=\"red\">Не сте въвели дата на раждане!</font>";
	}
	else
	{	
		if(checkdate($born_month, $born_day, $born_year))
		{
			$errors["born_date"] = true;
		}
		else
		{
		$errors["born_date"] = "<font color=\"red\">Въведената дата на раждане е невалидна!</font>";
		}
	}
	if(isset($_POST["gender"]))
	{
		$errors["gender"] = true;
		if($user->getGender() == "F")
		{
			$checked_f = "checked";
		}
		else
		{
			$checked_m = "checked";
		}
	}
	else
	{
		$errors["gender"] = "<font color=\"red\">Не сте въвели пол!</font>";
	}
	if(isset($_POST["terms"]))
	{
		$checked = "checked";
		$errors["terms"] = true;
	}
	else
	{
		$errors["terms"] = "<font color=\"red\">Не сте се съгласили с условията за ползване!</font>";
	}
	if($capcha == false) 
	{
		$errors["capcha"] = "<font color=\"red\">Невалиден код</font>";
	}
	else
	{
		$errors["capcha"] = true;
	}
	
	
	
	$no_errors = true;
	
	foreach ($errors as $error)
	{
		if($error !== true)
		{
		$no_errors = false;
		break;
		}
	}
	
	if($no_errors)
	{
		$db = new DBOperations();
		$db->add_user($user);
		$db->get_user_id_by_given_user_name_and_email($user);
		$key = $db->setActivationKey($user);
		
		$fromEmail = "admin@posmeise.com";
		$text="Здравейте ".$user->getUser_name().", вашия активиращ линк е: <a href=\"http://77.70.18.80/posmeise.com/activate.php?key=$key\">http://77.70.18.80/posmeise.com/activate.php?key=$key</a>";
		DBOperations::sendEmail($user->getEmail(),"Активация",$text);
		
		$reg_success = true;
	}
	else
	{
		$reg_success = false;
	}	
}
?>

		<script src="js/tooltip.js" type="text/javascript"></script>

		<?php 
		if(isset($reg_success) && $reg_success)
		{
			echo "Успешна регистрация! <a href=\"register.php\">Нова регистрация</a>";
		}
		else
		{
		?>
		<div>
			<center><h3>Регистрация</h3></center>
			<hr noshade="noshade" color="#0075ff"/>
			<center>
				<?php 
					if(isset($reg_success) && !$reg_success) echo "<font color=\"red\">Неуспешна регистрация!</font> Моля опитайте отново! ";
					if(isset($errors["capcha"])&& $errors["capcha"] !== true) echo $errors["capcha"];
				?>
			</center><br/>
			<form method="post" action="?view=register" id="form-login" name="regform" onsubmit="return full_check();">
			<table class="contentpaneopen" cellpadding="5" width="98%" border="0">
			<tbody>
				<tr height="40">
					<td align="right" width="24%">
						<label for="userid">Потребителско име: <span style="color: red;">*</span></label>
					</td>
					<td width="37%">
						<input style="width:185px;" class="inputbox" id="userid" type="text" name="user_name" value="<?php if(isset($_POST['user_name'])) echo $_POST['user_name'];?>" oninput="validate(this.value);" onblur="validate(this.value);" />
					</td>
					<td width="37%">
						<div id="userIdMessage"></div>
					</td>
				</tr>
				<tr>
					<td>
						&nbsp;
					</td>
					<td colspan="2">
						<p>
							Може да съдържа само латински букви (a-z, A-Z), цифри 0-9, “.”, “-” и “_”. Трябва да започва с буква. Дължина от 3 до 30 символа.
						</p>
					</td>
				</tr>
				<tr height="40">
					<td align="right">
						<label for="pass1">Парола: <span style="color: red;">*</span></label>
					</td>
					<td>
						<input style="width:185px;" class="inputbox" id="pass1" type="password" name="pass" value="" oninput="checkPassStrength(this.value);" />
					</td>
					<td>
						<div id="passMessage"></div>
					</td>
				</tr>
				<tr>
					<td>
						&nbsp;
					</td>
					<td colspan="2">
						<table cellpadding="0" cellspacing="0">
							<tr>
							  <td class="passstrstart">Слаба</td>
							  <td>
								<table cellpadding="0" cellspacing="0">
								  <tr>
									<td id="ps1" class="passstrength">&nbsp;</td>
									<td id="ps2" class="passstrength">&nbsp;</td>
									<td id="ps3" class="passstrength">&nbsp;</td>
									<td id="ps4" class="passstrength">&nbsp;</td>
									<td id="ps5" class="passstrength">&nbsp;</td>
									<td id="ps6" class="passstrength">&nbsp;</td>
									<td id="ps7" class="passstrength">&nbsp;</td>
									<td id="ps8" class="passstrength">&nbsp;</td>
									<td id="ps9" class="passstrength">&nbsp;</td>
								  </tr>
								</table>
							  </td>
							  <td class="passstrend">Сигурна</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td>
						&nbsp;
					</td>
					<td colspan="2">
						<p>
							Може да съдържа малки и големи латински букви, цифри и символи. Дължина от 6 до 30 символа. Не въвеждайте потребителското си име, други имена или дати за парола.
						</p>
					</td>
				</tr>
				<tr height="40">
					<td align="right">
						<label for="pass2">Повтори парола: <span style="color: red;">*</span></label>
					</td>
					<td>
						<input style="width:185px;" class="inputbox" id="pass2" type="password" name="pass2" value="" oninput="checkPassMatch(this.value);" />
					</td>
					<td>
						<div id="pass2Message"></div>
					</td>
				</tr>
				<tr height="40">
					<td align="right">
						<label for="email">E-mail: <span style="color: red;">*</span></label>
					</td>
					<td>
						<input style="width:185px;" class="inputbox" id="email" type="text" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" oninput="validateEmail(this.value);" onblur="validateEmail(this.value);" />
					</td>
					<td>
						<div id="EmailMessage"></div>
					</td>
				</tr>
				<tr>
					<td>
						&nbsp;
					</td>
					<td colspan="2">
						<p>
							Трябва да впишете съществуващ e-мейл, на който да получите активационен линк или известия в случай на забравено потребителско име или парола.
						</p>
					</td>
				</tr>
				<tr height="40">
					<td align="right">
						Дата на раждане: <span style="color: red;">*</span>
					</td>
					<td>
						<label class="select">
						<select style="width:39px;" class="styled_select" id="born_day" name="born_day" onchange="validateBorn(this.value,born_month.value,born_year.value);">
							<option style="color:#0075ff;" value="<?php if(isset($born_day)) echo $born_day; ?>"><?php if(isset($born_day)) echo $born_day; ?></option>
							<?php
								for ($i = 1; $i < 32; $i++)
								{
									echo '<option value="'.$i.'">'.$i.'</option>';
								}
							?>
						</select>
						</label>
						<label class="select">
						<select style="width:92px;" class="styled_select" id="born_month" name="born_month" onchange="validateBorn(born_day.value,this.value,born_year.value);">
							<option style="color:#0075ff;" value="<?php if(isset($born_month)) echo $born_month; ?>"><?php  if(isset($born_month)) echo $born_month_array[($born_month-1)]; ?></option>
							<?php
								for ($i = 0; $i < 12; $i++)
								{
									echo '<option value="'.($i+1).'">'.$born_month_array[$i].'</option>';
								}
							?>
						</select>
						</label>
						<label class="select">
						<select style="width:54px;" class="styled_select" id="born_year" name="born_year" onchange="validateBorn(born_day.value,born_month.value,this.value);">
							<option style="color:#0075ff;" value="<?php if(isset($born_year)) echo $born_year; ?>"><?php if(isset($born_year)) echo $born_year; ?></option>
							<?php
								for ($i = date('Y')-14; $i > date('Y')-101; $i--)
								{
									echo '<option value="'.$i.'">'.$i.'</option>';
								}
							?>
						</select>
						</label>
					</td>
					<td>
						<div id="BornMessage"></div>
					</td>
				</tr>
				<tr height="40">
					<td align="right">
						Пол: <span style="color: red;">*</span>
					</td>
					<td>
						<input class="checkbox" id="f" value="F" <?php if(isset($checked_f)) echo $checked_f;?> onclick="validateGender(this.checked,null);" name="gender" type="radio"/>
						<label for="f">Жена</label>&nbsp;&nbsp;&nbsp;
						<input class="checkbox" id="m" value="M" <?php if(isset($checked_m)) echo $checked_m;?> onclick="validateGender(null,this.checked);" name="gender" type="radio"/>
						<label for="m">Мъж</label>
					</td>
					<td>
						<div id="GenderMessage"></div>
					</td>
				</tr>
				<tr>
					<td align="right">
						
					</td>
					<td>
						<div style="width: 193px; height: 60px;">
							<div class="style_capcha">
								<img id="siimage" src="capcha/securimage_show.php?sid=<?php echo md5(time()) ?>" />
							</div>
							<!-- pass a session id to the query string of the script to prevent ie caching -->
							<a class="style_capcha" href="" onmouseover="Tip('Зареди нов код');" onmouseout="UnTip();" onclick="document.getElementById('siimage').src = 'capcha/securimage_show.php?sid=' + Math.random(); return false"><img src="capcha/images/refresh.gif" alt="Зареди нов код" border="0" onclick="this.blur()" /></a>
							<div style="width:30px;height:30px;" class="style_capcha" onmouseover="Tip('Прослушай кода');" onmouseout="UnTip();">
								<object id="capcha" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="30" height="30" id="SecurImage_as3">
									<param name="allowScriptAccess" value="sameDomain" />
									<param name="allowFullScreen" value="false" />
									<param name="movie" value="capcha/securimage_play.swf?audio=capcha/securimage_play.php&bgColor1=#bdd8f8&bgColor2=#bdd8f8&iconColor=#0075ff&roundedCorner=0" />
									<param name="quality" value="high" />
									<param name="bgcolor" value="#ffffff" />
									<embed src="capcha/securimage_play.swf?audio=capcha/securimage_play.php&bgColor1=#bdd8f8&bgColor2=#bdd8f8&iconColor=#0075ff&roundedCorner=0" quality="high" bgcolor="#ffffff" width="30" height="30" name="SecurImage_as3" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/>
								</object>
							</div>
						</div>
					</td>
					<td>
						
					</td>
				</tr>
				<tr height="40">
					<td align="right">
						<label for="pass2">Въведете кода: <span style="color: red;">*</span></label>
					</td>
					<td>
						<input style="width:185px;" class="inputbox" id="capcha" type="text" name="capcha_v" value="" onblur="checkCapcha(this.value);"/>
					</td>
					<td>
						<div id="capchaMessage"></div>
					</td>
				</tr>
				<tr>
					<td align="right">
						<span style="color: red;">*</span>
					</td>
					<td>
						<input class="checkbox" id="termsbox" type="checkbox" name="terms" value="terms" <?php if(isset($checked)) echo $checked;?> onclick="validateTerms(this.checked);validateGender(document.getElementById('f').checked,document.getElementById('m').checked);validateBorn(born_day.value,born_month.value,born_year.value);checkCapcha(capcha_v.value);" />
						<label for="termsbox"> Приемам <a href="terms.php" name="terms" target="_blank"><b>условията</b></a> на сайта.</label>
					</td>
					<td>
						<div id="TermsMessage"></div>
						
						<div id="boxes">
						<div class="window">
							<div class="close"><img align="right" width="16" height="16" src="img/close.gif" alt="X" title="Затвори" /></div>
							<iframe frameborder="no" src="terms.php"></iframe>
						</div>
						<div id="mask"></div>
						</div>
						
					</td>
				</tr>
			</tbody>
			</table>
			<table class="contentpaneopen" cellpadding="10" width="98%">
			<tbody>
				<tr>
					<td align="center">
						<input class="button" type="submit" name="reg" value="Регистрирай се"/>
					</td>
				</tr>
			</tbody>
			</table>
			</form>
			<hr noshade="noshade" color="#0075ff"/>
		</div>
		<?php 
			
		}
			
		?>